package net.realsee.service.wcs.task.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.task.WcsTaskStepParam;
import net.realsee.domain.wcs.task.bo.WcsTaskStepParamBo;
import net.realsee.domain.wcs.task.vo.WcsTaskStepParamVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.task.WcsTaskStepParamMapper;
import net.realsee.service.wcs.task.IWcsTaskStepParamService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 任务模板步骤参数Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsTaskStepParamServiceImpl extends ServiceImpl<WcsTaskStepParamMapper, WcsTaskStepParam> implements IWcsTaskStepParamService {

    private final WcsTaskStepParamMapper baseMapper;

    /**
     * 查询任务模板步骤参数
     */
    @Override
    public WcsTaskStepParamVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询任务模板步骤参数列表
     */
    @Override
    public TableDataInfo<WcsTaskStepParamVo> selectVoPage(WcsTaskStepParamBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsTaskStepParam> lqw = buildVoQueryWrapper(bo);
        Page<WcsTaskStepParamVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询任务模板步骤参数列表
     */
    @Override
    public List<WcsTaskStepParamVo> selectVoList(WcsTaskStepParamBo bo) {
        LambdaQueryWrapper<WcsTaskStepParam> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsTaskStepParam> buildVoQueryWrapper(WcsTaskStepParamBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsTaskStepParam> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsTaskStepParam::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskTempCode()), WcsTaskStepParam::getTaskTempCode, bo.getTaskTempCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStepCode()), WcsTaskStepParam::getStepCode, bo.getStepCode());
        lqw.eq(StringUtils.isNotBlank(bo.getParamCode()), WcsTaskStepParam::getParamCode, bo.getParamCode());
        lqw.eq(StringUtils.isNotBlank(bo.getValueType()), WcsTaskStepParam::getValueType, bo.getValueType());
        lqw.eq(StringUtils.isNotBlank(bo.getValue()), WcsTaskStepParam::getValue, bo.getValue());
        lqw.like(StringUtils.isNotBlank(bo.getVaribleName()), WcsTaskStepParam::getVaribleName, bo.getVaribleName());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsTaskStepParam::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsTaskStepParam::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsTaskStepParam::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsTaskStepParam::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsTaskStepParam::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsTaskStepParam::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsTaskStepParam::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsTaskStepParam::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsTaskStepParam::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsTaskStepParam::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsTaskStepParam::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsTaskStepParam::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsTaskStepParam::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增任务模板步骤参数
     */
    @Override
    public Boolean insertByBo(WcsTaskStepParamBo bo) {
        WcsTaskStepParam add = BeanUtil.toBean(bo, WcsTaskStepParam.class);
        return this.save(add);
    }

    /**
     * 修改任务模板步骤参数
     */
    @Override
    public Boolean updateByBo(WcsTaskStepParamBo bo) {
        WcsTaskStepParam update = BeanUtil.toBean(bo, WcsTaskStepParam.class);
        return this.updateById(update);
    }

    /**
     * 批量删除任务模板步骤参数
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
